{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "de765f16-341b-4dc3-b59d-b16437c7e050",
   "metadata": {},
   "source": [
    "\n",
    "# RAG with structurizr sample codes downloaded from github\n",
    "\n",
    "Please refer to the content of dsl code below in this demo.\n",
    "\n",
    "[examples/dsl/big-bank-plc/model.dsl](examples/dsl/big-bank-plc/model.dsl)\n",
    "\n",
    "The code is clone from `https://github.com/structurizr/examples.git`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e25cdbdc-daee-4a9b-b363-9080dc216927",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name: langchain\n",
      "Version: 0.1.9\n",
      "Summary: Building applications with LLMs through composability\n",
      "Home-page: https://github.com/langchain-ai/langchain\n",
      "Author: \n",
      "Author-email: \n",
      "License: MIT\n",
      "Location: /opt/conda/lib/python3.11/site-packages\n",
      "Requires: aiohttp, dataclasses-json, jsonpatch, langchain-community, langchain-core, langsmith, numpy, pydantic, PyYAML, requests, SQLAlchemy, tenacity\n",
      "Required-by: \n"
     ]
    }
   ],
   "source": [
    "!pip show langchain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e07b2c7d-62d7-457c-bbcb-298b29aa522c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Please define the following env variables in the .env file \n",
    "# \n",
    "# ollama_url = \"http://ollama:11434\"\n",
    "# model_name = \"mistral:instruct\"\n",
    "# "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fa2048ea-dbc4-49d7-b855-8b546dce2edb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Use the utility.py to get llm, embedding and output parser\n",
    "import utility\n",
    "embeddings = utility.get_embeddings()\n",
    "llm = utility.get_llm()\n",
    "output_parser = utility.get_output_parser()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a11baec0-71af-46f8-bb39-bacc1c354c55",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1/1 [00:00<00:00, 1017.29it/s]\n"
     ]
    }
   ],
   "source": [
    "# Load the dsl file from local machine for embedding\n",
    "# https://python.langchain.com/docs/modules/data_connection/document_loaders/file_directory\n",
    "# https://python.langchain.com/docs/integrations/document_loaders/unstructured_file\n",
    "\n",
    "from langchain_community.document_loaders import DirectoryLoader, TextLoader\n",
    "\n",
    "# only load examples/dsl/big-bank-plc/model.dsl\n",
    "loader = DirectoryLoader('examples/dsl/big-bank-plc', glob=\"model.dsl\", show_progress=True, use_multithreading=True, loader_cls=TextLoader)\n",
    "\n",
    "docs = loader.load()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "97c4e0ee-d1b7-4f11-914c-4b19d7812b06",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Document(page_content='workspace {\\n\\n    model {\\n        customer = person \"Personal Banking Customer\" \"A customer of the bank, with personal bank accounts.\" \"Customer\"\\n\\n        group \"Big Bank plc\" {\\n            supportStaff = person \"Customer Service Staff\" \"Customer service staff within the bank.\" \"Bank Staff\"\\n            backoffice = person \"Back Office Staff\" \"Administration and support staff within the bank.\" \"Bank Staff\"\\n\\n            mainframe = softwaresystem \"Mainframe Banking System\" \"Stores all of the core banking information about customers, accounts, transactions, etc.\" \"Existing System\"\\n            email = softwaresystem \"E-mail System\" \"The internal Microsoft Exchange e-mail system.\" \"Existing System\"\\n            atm = softwaresystem \"ATM\" \"Allows customers to withdraw cash.\" \"Existing System\"\\n\\n            internetBankingSystem = softwaresystem \"Internet Banking System\" \"Allows customers to view information about their bank accounts, and make payments.\"\\n        }\\n\\n        # relationships between people and software systems\\n        customer -> internetBankingSystem \"Views account balances, and makes payments using\"\\n        internetBankingSystem -> mainframe \"Gets account information from, and makes payments using\"\\n        internetBankingSystem -> email \"Sends e-mail using\"\\n        email -> customer \"Sends e-mails to\"\\n        customer -> supportStaff \"Asks questions to\" \"Telephone\"\\n        supportStaff -> mainframe \"Uses\"\\n        customer -> atm \"Withdraws cash using\"\\n        atm -> mainframe \"Uses\"\\n        backoffice -> mainframe \"Uses\"\\n    }\\n\\n}', metadata={'source': 'examples/dsl/big-bank-plc/model.dsl'})]\n"
     ]
    }
   ],
   "source": [
    "# print the loaded documents, the model.dsl content has been loaded as the page_content\n",
    "print(docs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "dea60994-7061-48d3-a1de-8617f447078e",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Created a chunk of size 261, which is longer than the specified 200\n",
      "Created a chunk of size 386, which is longer than the specified 200\n",
      "Created a chunk of size 588, which is longer than the specified 200\n"
     ]
    }
   ],
   "source": [
    "# Split the document content into small chunks of texts for embedding\n",
    "\n",
    "from langchain.text_splitter import CharacterTextSplitter\n",
    "text_splitter = CharacterTextSplitter(chunk_size=200, chunk_overlap=50)\n",
    "texts = text_splitter.split_documents(docs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4070da74-d1c9-4158-8684-2c8c243993de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Document(page_content='workspace {\\n\\n    model {\\n        customer = person \"Personal Banking Customer\" \"A customer of the bank, with personal bank accounts.\" \"Customer\"', metadata={'source': 'examples/dsl/big-bank-plc/model.dsl'}), Document(page_content='group \"Big Bank plc\" {\\n            supportStaff = person \"Customer Service Staff\" \"Customer service staff within the bank.\" \"Bank Staff\"\\n            backoffice = person \"Back Office Staff\" \"Administration and support staff within the bank.\" \"Bank Staff\"', metadata={'source': 'examples/dsl/big-bank-plc/model.dsl'}), Document(page_content='mainframe = softwaresystem \"Mainframe Banking System\" \"Stores all of the core banking information about customers, accounts, transactions, etc.\" \"Existing System\"\\n            email = softwaresystem \"E-mail System\" \"The internal Microsoft Exchange e-mail system.\" \"Existing System\"\\n            atm = softwaresystem \"ATM\" \"Allows customers to withdraw cash.\" \"Existing System\"', metadata={'source': 'examples/dsl/big-bank-plc/model.dsl'}), Document(page_content='internetBankingSystem = softwaresystem \"Internet Banking System\" \"Allows customers to view information about their bank accounts, and make payments.\"\\n        }', metadata={'source': 'examples/dsl/big-bank-plc/model.dsl'}), Document(page_content='# relationships between people and software systems\\n        customer -> internetBankingSystem \"Views account balances, and makes payments using\"\\n        internetBankingSystem -> mainframe \"Gets account information from, and makes payments using\"\\n        internetBankingSystem -> email \"Sends e-mail using\"\\n        email -> customer \"Sends e-mails to\"\\n        customer -> supportStaff \"Asks questions to\" \"Telephone\"\\n        supportStaff -> mainframe \"Uses\"\\n        customer -> atm \"Withdraws cash using\"\\n        atm -> mainframe \"Uses\"\\n        backoffice -> mainframe \"Uses\"\\n    }', metadata={'source': 'examples/dsl/big-bank-plc/model.dsl'}), Document(page_content='}', metadata={'source': 'examples/dsl/big-bank-plc/model.dsl'})]\n"
     ]
    }
   ],
   "source": [
    "# print the chucks, the model.dsl content has been splitted into small documents with page_content of \n",
    "print(texts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "93f03020-3887-4aae-9ece-6bd088d51d49",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "from langchain_community.vectorstores import DocArrayInMemorySearch\n",
    "\n",
    "vectorstore = DocArrayInMemorySearch.from_documents(texts, embeddings)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "02b3f72b-a7e1-48f9-8a08-e61399644775",
   "metadata": {},
   "outputs": [],
   "source": [
    "retriever = vectorstore.as_retriever()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "081c8e88-e45c-4b32-bc0e-206801565f83",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.runnables import RunnableParallel, RunnablePassthrough\n",
    "\n",
    "retrieval = RunnableParallel(\n",
    "    {\"context\": retriever, \"input\": RunnablePassthrough()}\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "dd527cf0-379d-4800-bff7-02b7cfdf41c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "\n",
    "prompt = ChatPromptTemplate.from_messages([\n",
    "    (\"system\", \"You are a software architect with knowledage in structurizr helping software architecture modeling and documenation in the following context: {context}\"),\n",
    "    (\"user\", \"{input}\")\n",
    "])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "fd5a6f70-6f06-4360-b35f-8a5509c36bc8",
   "metadata": {},
   "outputs": [],
   "source": [
    "chain = retrieval | prompt | llm | output_parser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "336770a5-a375-4199-9759-bf1753ff774a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " The \"Internet Banking System\" is a software system described in the provided documentation. It is defined as a separate entity within the given model, which allows customers to view information about their bank accounts and make payments online. This system interacts with other entities such as customers and the core banking information system (Mainframe Banking System) to provide its functionalities.\n"
     ]
    }
   ],
   "source": [
    "result = chain.invoke(\"\"\"What is the \"Internet Banking System\" \"\"\")\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "b2ec70bc-9810-4af9-af64-0a507958cc83",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Based on the provided context, the \"E-mail System\" referred to in the given software architecture model is represented by the `email` software system defined in the `model.dsl` file. This system is labeled as an \"Existing System,\" suggesting it's a preexisting part of the current banking application landscape. However, no further details about its functionality or integration with other components are explicitly stated within the provided model snippets."
     ]
    }
   ],
   "source": [
    "# print(chain.invoke(\"\"\"What is the \"E-mail System\" \"\"\"))\n",
    "for chunk in chain.stream(\"\"\"What is the \"E-mail System\" \"\"\"):\n",
    "    print(chunk, end=\"\", flush=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "23a2835a-bd0e-42ad-af42-8b05afb09906",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Based on the provided context from the given Structurizr model DSL documents, there are two defined types of bank staff: Customer Service Staff and Back Office Staff. Both are defined as persons under their respective groups in the Big Bank plc model. Therefore, the bank staff are represented as \"Customer Service Staff\" and \"Back Office Staff\" within the given context.\n"
     ]
    }
   ],
   "source": [
    "# sample to run a batch of queries\n",
    "for result in chain.batch([\"\"\"Who are the bank staff \"\"\"]):\n",
    "    print(result,flush=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "8ff5e365-ecf9-49c9-bef7-fb522067074f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " In the given context, there isn't an exact match for an \"Internet Banking System\" defined in the provided DSL (Domain Specific Language) code. You can add it as a new software system under the workspace or any other appropriate place based on your architecture design.\n",
      "\n",
      "Here is an example of how you could define an \"Internet Banking System\":\n",
      "\n",
      "```\n",
      "document model \"Big Bank plc\" {\n",
      "    workspace {\n",
      "        // ... existing definitions ...\n",
      "    }\n",
      "\n",
      "    softwareSystem \"Internet Banking System\" {\n",
      "        name \"Internet Banking\"\n",
      "        description \"The web-based banking platform for customers to access their accounts online.\"\n",
      "    }\n",
      "}\n",
      "```\n",
      "\n",
      "Keep in mind, this is just the definition of the Internet Banking System. Depending on your architecture design, you might want to add more details such as interfaces, interactions with other systems, and any additional components or services that are part of the system.\n"
     ]
    }
   ],
   "source": [
    "print(chain.invoke(\"\"\" What is the dsl code of \"Internet Banking System\" \"\"\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4854be4a-d20b-4d1f-9afc-2d8f144b6595",
   "metadata": {},
   "source": [
    "## Notes\n",
    "\n",
    "The result is quite good and it can generate the structurizr DSL code below\n",
    "\n",
    "```markdown\n",
    "softwareSystem \"Internet Banking System\" {\n",
    "        name \"Internet Banking\"\n",
    "        description \"The web-based banking platform for customers to access their accounts online.\"\n",
    "    }\n",
    "```\n",
    "\n",
    "Please refer to the original structurizr DSL code in the model.dsl\n",
    "\n",
    "```markdown\n",
    "internetBankingSystem = softwaresystem \"Internet Banking System\" \"Allows customers to view information about their bank accounts, and make payments.\"\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "bb97e7f1-25c3-49d5-ae31-6583d1a8bd45",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Here is the updated code snippet based on your description for the \"Internet Banking System\":\n",
      "\n",
      "```markdown\n",
      "internetBankingSystem = softwaresystem \"Internet Banking System\" \"Allows customers to view information about their bank accounts, and make payments.\"\n",
      "```\n"
     ]
    }
   ],
   "source": [
    "instruct = \"\"\" please generate the code again with the description below for the \"Internet Banking System\".\n",
    "description: \"Allows customers to view information about their bank accounts, and make payments.\"\n",
    "\"\"\"\n",
    "print(chain.invoke(instruct))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "537083fb-9634-4d9d-bd6e-641bf3f32dba",
   "metadata": {},
   "source": [
    "## Notes\n",
    "\n",
    "it generates the extract structurizr DSL code previusely defined in the model.dsl\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "06c16799-09a1-4b20-a3e8-70a73e8a6fd3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Based on your requirements, here's the Structurizr DSL code:\n",
      "\n",
      "```dsl\n",
      "softwaresystem BigBankPlc {\n",
      "  name \"Big Bank plc\"\n",
      "\n",
      "  softwareSystem InternetBankingSystem {\n",
      "    name \"Internet Banking System\"\n",
      "    description \"Allows customers to view information about their bank accounts and make payments.\"\n",
      "\n",
      "    relationship customer {\n",
      "      endCustomer\n",
      "      role \"Uses\"\n",
      "      name \"Accesses internet banking services\"\n",
      "    }\n",
      "\n",
      "    relationship mainframe {\n",
      "      dependency\n",
      "      cardinality 1..1\n",
      "      name \"Communicates with the mainframe system for account information and making payments\"\n",
      "    }\n",
      "\n",
      "    relationship email {\n",
      "      dependency\n",
      "      cardinality 1..1\n",
      "      name \"Sends e-mails to customers using the internal email system\"\n",
      "    }\n",
      "  }\n",
      "\n",
      "  softwareSystem Mainframe {\n",
      "    name \"Mainframe Banking System\"\n",
      "    description \"Stores all of the core banking information about customers, accounts, transactions, etc.\"\n",
      "    description \"Existing System\"\n",
      "\n",
      "    relationship InternetBankingSystem {\n",
      "      dependency\n",
      "      cardinality 1..1\n",
      "      name \"Provides account information and supports payment processing\"\n",
      "    }\n",
      "\n",
      "    softwareSystem Backoffice {\n",
      "      name \"Back Office System\"\n",
      "      description \"Used by bank staff to search customer information.\"\n",
      "      description \"Existing System\"\n",
      "\n",
      "      relationship this {\n",
      "        dependency\n",
      "        cardinality 1..1\n",
      "        name \"Queries the mainframe system for customer data\"\n",
      "      }\n",
      "    }\n",
      "  }\n",
      "\n",
      "  softwareSystem ATM {\n",
      "    name \"ATM\"\n",
      "    description \"Allows customers to withdraw cash.\"\n",
      "    description \"Existing System\"\n",
      "\n",
      "    relationship customer {\n",
      "      endCustomer\n",
      "      role \"Uses\"\n",
      "      name \"Withdraws cash from the ATM machine\"\n",
      "    }\n",
      "\n",
      "    relationship mainframe {\n",
      "      dependency\n",
      "      cardinality 1..1\n",
      "      name \"Communicates with the mainframe system to verify account information and authorize transactions\"\n",
      "    }\n",
      "  }\n",
      "}\n",
      "```\n",
      "\n",
      "This code describes a Big Bank plc software architecture where customers can use an ATM to withdraw cash, while bank staff uses the mainframe system for searching customer information. The Internet Banking System acts as a mediator between the Customer and Mainframe systems.\n"
     ]
    }
   ],
   "source": [
    "instruct = \"\"\" Please generate the structurizr DSL code based on the requirements below:\n",
    "The \"Big Bank plc\" wants to create a new application for its bank staff and customers, \n",
    "the customers will use \"ATM\" system to withdraw cash, the bank staff will use mainframe system to search customer information.\n",
    "\"\"\"\n",
    "print(chain.invoke(instruct))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "09df3b1e-40ee-4f2e-926b-9c722395328a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Based on your requirements, here's the Structurizr DSL code generated from the provided context:\n",
      "\n",
      "```dsl\n",
      "workspace {\n",
      "    name \"Internet Banking System\"\n",
      "    description \"Allows customers to view information about their bank accounts and make payments.\"\n",
      "\n",
      "    model {\n",
      "        softwareSystem internetBankingSystem {\n",
      "            name \"Internet Banking System\"\n",
      "            description \"Allows customers to view information about their bank accounts and make payments.\"\n",
      "        }\n",
      "\n",
      "        softwareSystem mainframe {\n",
      "            name \"Mainframe Banking System\"\n",
      "            description \"Stores all of the core banking information about customers, accounts, transactions, etc. (Existing System)\"\n",
      "        }\n",
      "\n",
      "        softwareSystem email {\n",
      "            name \"E-mail System\"\n",
      "            description \"The internal Microsoft Exchange e-mail system. (Existing System)\"\n",
      "        }\n",
      "\n",
      "        softwareSystem atm {\n",
      "            name \"ATM\"\n",
      "            description \"Allows customers to withdraw cash. (Existing System)\"\n",
      "        }\n",
      "\n",
      "        internetBankingSystem -> mainframe \"Gets account information from, and makes payments using\"\n",
      "        internetBankingSystem -> email \"Sends e-mails using\"\n",
      "\n",
      "        customer --> internetBankingSystem \"Views account balances, and makes payments using\"\n",
      "        customer --> supportStaff \"Asks questions to (Telephone)\"\n",
      "        customer --> atm \"Withdraws cash using\"\n",
      "\n",
      "        mainframe --> backoffice \"Uses\"\n",
      "    }\n",
      "}\n",
      "```\n",
      "\n",
      "This code adheres to the specified format, starting with `workspace {`, ending with `}`, having a `model {}` section for defining software systems and relationships, and following the `systemA -> systemB` pattern for defining relationships.\n"
     ]
    }
   ],
   "source": [
    "instruct = \"\"\" please generate the Structurizr DSL code again with the following formats:\n",
    "\n",
    "1. it must start with \"workspace {\" as the first line\n",
    "2. it must end with \"}\" as the last line\n",
    "3. it must have a section called \"model\"\n",
    "4. it must follow pattern \"systemA -> systemB\" to define relationship\n",
    "5. it must not embeb \"relationship\" within \"softwareSystem\"\n",
    "6. it must adhere to the template below:\n",
    "\"workspace {\n",
    "    name \"placehoder for application name\"\n",
    "    descritption \"placehoder for application descritption\"\n",
    "\n",
    "    model {\n",
    "        //define the software systems here\n",
    "\n",
    "        //define the relationship here\n",
    "    }\n",
    "}\"\n",
    "\"\"\"\n",
    "print(chain.invoke(instruct))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "beb7c2f6-21f2-4220-ba67-82c7394775fd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " workspace {\n",
      "    name \"Internet Banking System\"\n",
      "    description \"Allows customers to view information about their bank accounts, and make payments.\"\n",
      "\n",
      "    model {\n",
      "        customer = person \"Customer\"\n",
      "        internetBankingSystem = softwareSystem \"Internet Banking System\" {\n",
      "            views {\n",
      "                systemContext {\n",
      "                    include *\n",
      "                    autolayout lr\n",
      "                }\n",
      "            }\n",
      "            container {\n",
      "                webApp = container \"Web Application\" {\n",
      "                    // Define web app components here\n",
      "                }\n",
      "            }\n",
      "        }\n",
      "        mainframe = softwareSystem \"Mainframe Banking System\" {\n",
      "            views {\n",
      "                container {\n",
      "                    // Define mainframe components here\n",
      "                }\n",
      "            }\n",
      "        }\n",
      "        email = softwareSystem \"Email System\" {\n",
      "            views {\n",
      "                container {\n",
      "                    // Define email system components here\n",
      "                }\n",
      "            }\n",
      "        }\n",
      "        atm = softwareSystem \"ATM\" {\n",
      "            views {\n",
      "                container {\n",
      "                    // Define ATM components here\n",
      "                }\n",
      "            }\n",
      "        }\n",
      "\n",
      "        customer -> internetBankingSystem \"Uses\"\n",
      "        internetBankingSystem -> mainframe \"Gets account information from, and makes payments using\"\n",
      "        internetBankingSystem -> email \"Sends e-mails using\"\n",
      "        customer -> supportStaff \"Asks questions to\"\n",
      "        supportStaff -> mainframe \"Uses\"\n",
      "        customer -> atm \"Withdraws cash using\"\n",
      "        atm -> mainframe \"Uses\"\n",
      "        backoffice -> mainframe \"Uses\"\n",
      "    }\n",
      "\n",
      "    views {\n",
      "        systemContext internetBankingSystem {\n",
      "            include *\n",
      "            autolayout lr\n",
      "        }\n",
      "\n",
      "        container internetBankingSystem {\n",
      "            include *\n",
      "            autolayout lr\n",
      "        }\n",
      "\n",
      "        theme default\n",
      "    }\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "instruct = \"\"\" please generate the Structurizr DSL code again with the following formats:\n",
    "1. it must use this pattern to define softwareSystem, pattern: id = softwareSystem \"Software System Name\" \n",
    "2. it must have a section called \"views\" and define the systemContext\n",
    "3. it must adhere to the template below:\n",
    "\"workspace {\n",
    "    name \"placehoder for application name\"\n",
    "    descritption \"placehoder for application descritption\"\n",
    "\n",
    "    model {\n",
    "        //define the persons here\n",
    "        user = person \"User\"\n",
    "        \n",
    "        //define the software systems here, it can have multiple systems\n",
    "        softwareSystem = softwareSystem \"Software System\" {\n",
    "            webapp = container \"Web Application\" {\n",
    "                \n",
    "            }\n",
    "            database = container \"Database\" {\n",
    "                \n",
    "            }\n",
    "        }\n",
    "        //define the relationship here\n",
    "        user -> webapp \"Uses\"\n",
    "        webapp -> database \"Reads from and writes to\"\n",
    "        \n",
    "    }\n",
    "\n",
    "    views {\n",
    "        systemContext softwareSystem {\n",
    "            include *\n",
    "            autolayout lr\n",
    "        }\n",
    "\n",
    "        container softwareSystem {\n",
    "            include *\n",
    "            autolayout lr\n",
    "        }\n",
    "\n",
    "        theme default\n",
    "    }\n",
    "\n",
    "}\"\n",
    "\"\"\"\n",
    "\n",
    "# print(chain.invoke(instruct))\n",
    "for chunk in chain.stream(instruct):\n",
    "    print(chunk, end=\"\", flush=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31f7589d-aa07-486f-9ee2-ac2070a7f1cf",
   "metadata": {},
   "source": [
    "https://blog.csdn.net/weixin_42608414/article/details/131633340"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
